'''
Created on Apr 20, 2011

@author: posttool
''' 
from impact import platform, test, widget,dashboard
from google.appengine.ext.db.metadata import Kind
import queue
import logging

def get_name():
	return __name__

def get_url_mappings():
		return [
				('/admin', AdminPage),
				('/api/admin/(.*)', AdminApi)
			   ]

class AdminPage(platform.TemplateHandler):

	def __init__(self):
		self.template = "templates/admin.html"

	def prepare_page(self,uctx,inputs,pagedata):
		pass


class AdminApi(platform.ServiceHandler):

	def __init__(self):
		pass

	def delete_all_data(self,uctx,args,params):
		#platform.delete_all_data()
		kk = Kind.all()
		for kind in kk:
			if kind.kind_name.startswith("_") or kind.kind_name.startswith("WidgetRegistry") or kind.kind_name.startswith("WidgetCategory"):
				logging.info("SKIPPING "+kind.kind_name+" IN ADMIN DELETE ALL")
				continue
			logging.info("DELETEING "+kind.kind_name)
			queue.page_apply(kind,queue.page_apply_delete, callback_page_size=2, page_size=48,multiplex=False)

		return {'message':'running'}

	def install_widgets(self,uctx,args,params):
		widget.init_root_and_registry()
		return {'message':'ok'}

	def create_user_and_test_data(self,uctx,args,params):
		test.create_user_and_test_data()
		return {'message':'ok'}

	def run_widget_data_refresh(self,uctx,args,params):
		dashboard.process_all_widget_instances()



